package com.example.rightmost.service.Impl;

import com.example.rightmost.dao.CategoryMapper;
import com.example.rightmost.entity.Result;
import com.example.rightmost.entity.TopicCategory;
import com.example.rightmost.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:javayhch
 * @date:2024/8/25
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    public CategoryMapper categoryMapper;

    @Override
    public List<TopicCategory> getCategoryList(TopicCategory category) {
        return categoryMapper.getCategoryList(category);
    }

    @Override
    public Result deleteCategory(Integer id) {
        int i = categoryMapper.deleteCategory(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    @Override
    public Result updateStatus(Integer id) {
        int i = categoryMapper.updateStatus(id);
        if(i > 0){
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }

    @Override
    public Result addCategory(String name) {
        TopicCategory getCategory = categoryMapper.selectCategory(name);
        System.out.println(getCategory);
        if (getCategory == null){
            LocalDateTime createTime = LocalDateTime.now();
            int i = categoryMapper.addCategory(name, createTime);
            if(i > 0){
                return Result.success();
            }else{
                return Result.error("添加失败");
            }
        } else {
            return Result.error("此分类已存在");
        }
    }

    @Override
    public Result updateCategory(TopicCategory category) {
        TopicCategory getCategory = categoryMapper.selectCategory(category.getName());
        if (getCategory == null){
            category.setCreateTime(LocalDateTime.now());
            int i = categoryMapper.updateCategory(category);
            if(i > 0){
                return Result.success();
            }else{
                return Result.error("修改失败");
            }
        } else {
            return Result.error("此分类已存在");
        }
    }

    //gao
    @Override
    public List<TopicCategory> getTabs() {
        return categoryMapper.getTabs();
    }

}
